{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import k3d\n",
    "import numpy as np\n",
    "\n",
    "iteration = 4\n",
    "size = 3**iteration\n",
    "\n",
    "voxels = np.ones((size, size, size));\n",
    "\n",
    "def iterate(length, x, y, z):\n",
    "    \n",
    "    nl = length // 3    \n",
    "    \n",
    "    if nl < 1:\n",
    "        return\n",
    "    \n",
    "    margin = (nl-1) // 2\n",
    "\n",
    "    voxels[z-margin:z+margin+1, y-margin:y+margin+1, :] = 0\n",
    "    voxels[z-margin:z+margin+1, :, x-margin:x+margin+1] = 0\n",
    "    voxels[:, y-margin:y+margin+1, x-margin:x+margin+1] = 0    \n",
    "    \n",
    "    for ix,iy,iz in np.ndindex((3,3,3)):\n",
    "        if (1 if ix !=1 else 0) + (1 if iy != 1 else 0) + (1 if iz != 1 else 0) !=2:\n",
    "            iterate(nl, x + (ix-1) * nl, y + (iy-1) * nl , z + (iz-1) * nl)\n",
    "\n",
    "iterate(size, size//2, size//2, size//2)\n",
    "\n",
    "color_map = (0xffff00)\n",
    "plot = k3d.plot(name='Sponge')\n",
    "plot += k3d.voxels(voxels.astype(np.uint8), color_map,name='Menger_sponge')\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.camera = [49.74392405695982,\n",
    " -2.9524769249925455,\n",
    " 27.185488598217045,\n",
    " 43.65220601477353,\n",
    " 19.109636932275787,\n",
    " 9.719259554462269,\n",
    " -0.19179675209811095,\n",
    " 0.5291470859180342,\n",
    " 0.8265696385357213]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.clipping_planes = [[0, 0, -1, 0.15]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.clipping_planes = [\n",
    "    [0, 0, -1, 0.15],\n",
    "    [-0.5931578442826305,\n",
    "    0.6811078258641979,\n",
    "    -0.42925039465622833,\n",
    "    0.09701107626276362]\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.clipping_planes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "nbTranslate": {
   "displayLangs": [
    "en",
    "pl"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "pl",
   "targetLang": "en",
   "useGoogleTranslate": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
